package com.jzr.bedside.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jzr.bedside.db1.model.TFoodMenu;
import com.jzr.bedside.db1.service.TFoodMenuService;
import com.jzr.common.entity.vo.Result;
import com.jzr.common.util.U;
import com.jzr.common.util.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/foodMenu")
@Api(tags = "食堂菜单")
public class FoodMenuController {
    @Value("${file.foodPic-download-url}")
    private String downloadUrl;

    @Autowired
    private TFoodMenuService foodMenuService;

    @PostMapping("/get")
    @ApiOperation("获取食堂菜单列表")
    public Result<List<TFoodMenu>> get(@ApiParam("查询参数") @RequestBody TFoodMenu record){
        QueryWrapper<TFoodMenu> qw = new QueryWrapper<>();
        qw.orderByAsc("type","name");
        if(record != null){
            if(U.isNotBlank(record.getName())){
                qw.likeRight(TFoodMenu.Name_,record.getName());
            }
            if(U.isNotBlank(record.getPrice())){
                qw.eq(TFoodMenu.Price_,record.getPrice());
            }
            if(U.isNotBlank(record.getType())){
                qw.eq(TFoodMenu.Type_,record.getType());
            }
        }
        qw.eq(TFoodMenu.Status_,1);

        List<TFoodMenu> list = foodMenuService.list(qw);
        if(list != null){
            for(TFoodMenu m : list){
                if(U.isNotBlank(m.getImgUrl())) {
                    m.setImgUrl(UrlUtil.concat(downloadUrl, m.getImgUrl()));
                }
            }
        }
        return Result.success("成功",list, list == null || list.isEmpty()? 0 : list.size());
    }
}
